﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MovieRental.XmlDataProviderCore;
using MovieRental.DataInterfaces;
using MovieRental.Entities;
using MovieRental.GeneralDataExceptions;

namespace MovieRental.XmlRepositoryLibrary
{
    public class XmlLoanCartRepository : XmlRepository<LoanCartInfo>, ILoanCartRepository
    {
        public XmlLoanCartRepository(XmlDataProvider provider)
            : base(provider)
        {
        }

        public LoanCartInfo Load(Guid id)
        {
            using (var context = CreateContext())
            {
                var set = context.GetAllForType(typeof(LoanCartInfo)).Select(x => (LoanCartInfo)x);
                try
                {
                    return set.Single(x => x.Id == id);
                }
                catch (Exception ex)
                {
                    throw new ItemNotFoundException(id.ToString(), ex);
                }
            }
        }

        public void Delete(Guid id)
        {
            LoanCartInfo loanCart = new LoanCartInfo(id);
            Delete(loanCart);
        }

        public IEnumerable<LoanCartInfo> LoadAllForDay(DateTime date)
        {
            return LoadAll().Where(x => x.LoanDate.Date == date.Date);
        }

        public IEnumerable<LoanCartInfo> LoadAllForCustomer(Guid customerId)
        {
            return LoadAll().Where(x => x.CustomerId == customerId);
        }
    }
}
